package com.zxf.course.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.zxf.course.bean.Student;

public class UserLoginInterceptor implements HandlerInterceptor {

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {

		String requestUrl = request.getRequestURI();
		boolean contains1 = requestUrl.contains("/login");
		boolean contains2 = requestUrl.contains("/studentLogin");
		boolean contains3 = requestUrl.contains("/register");
		boolean contains4 = requestUrl.contains("/registerStudent");
		String contextPath = request.getContextPath();
		if (!contains1) {
			if (!contains2) {
				if(!contains3) {
					if(!contains4) {
						Student student = (Student) request.getSession().getAttribute("student");
						
						if (null == student) {
							response.sendRedirect(contextPath + "/student/login");
							return false;
						}
					}
				}
			}
		}

		return true;
	}

	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
			throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		// TODO Auto-generated method stub

	}
}
